package nero.yncc.com.createart.utils;

import android.app.ActivityManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.text.SimpleDateFormat;
import java.util.Date;

import nero.yncc.com.createart.base.BaseApplication;

/**
 * Created by nero on 2017/5/25.
 */

public class SomeUtil {

    public static String timespanTotimeStr(String timespan) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/dd/MM HH:mm");
        return sdf.format(new Date(Long.parseLong(timespan + "000")));
    }

    public static String timespanTotimeStrShort(String timespan) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/dd/MM");
        return sdf.format(new Date(Long.parseLong(timespan + "000")));
    }

    public static String strNeed(String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        } else {
            return str;
        }
    }

    public static String getRunningActivityName() {
        ActivityManager activityManager = (ActivityManager) BaseApplication.mContext.getSystemService(BaseApplication.mContext.ACTIVITY_SERVICE);
        String runningActivity = activityManager.getRunningTasks(1).get(0).topActivity.getClassName();
        return runningActivity;
    }

    public static void setListViewHeightBasedOnChildren333(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        int height_cache = 0;
        View listItem = listAdapter.getView(0, null, listView);
        listItem.measure(0, 0); // 计算子项View 的宽高
        height_cache = listItem.getMeasuredHeight(); // 统计所有子项的总高度
        totalHeight = height_cache * listAdapter.getCount();


        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }

    public static  void setListViewHeightBasedOnChildren(ListView listView) {

        ListAdapter listAdapter = listView.getAdapter();

        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;

        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();

        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));

        ((ViewGroup.MarginLayoutParams) params).setMargins(10, 10, 10, 10); // 可删除

        listView.setLayoutParams(params);
    }

    public static  void setListViewHeightBasedOnChildren(RecyclerView listView,int height) {

        RecyclerView.Adapter listAdapter = listView.getAdapter();

        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        totalHeight+=listAdapter.getItemCount()*height;
//        for (int i = 0; i < listAdapter.getItemCount(); i++) {
//            View listItem = listAdapter.(i, null, listView);
//            listItem.measure(0, 0);
//            totalHeight += listItem.getMeasuredHeight();
//        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();

        params.height = totalHeight;
//                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));

        ((ViewGroup.MarginLayoutParams) params).setMargins(10, 10, 10, 10); // 可删除

        listView.setLayoutParams(params);
    }

    public static  void setListViewHeightBasedOnChildrenMore(ListView listView,int more) {

        ListAdapter listAdapter = listView.getAdapter();

        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;

        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();

        params.height = more+totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));

        ((ViewGroup.MarginLayoutParams) params).setMargins(10, 10, 10, 10); // 可删除

        listView.setLayoutParams(params);
    }

    public static  void setListViewHeightBasedOnChildrenNum(ListView listView,int num) {

        ListAdapter listAdapter = listView.getAdapter();

        if (listAdapter == null) {
            return;
        }


        ViewGroup.LayoutParams params = listView.getLayoutParams();

        params.height =num;


        listView.setLayoutParams(params);
    }


}
